﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

namespace DevEx.Thanh.Class
{
    class CMenuController
    {
        QLVTDataContext objDataContext;
        public IQueryable<tblMenu> SelectAllRecord()
	    {
		    try {
			    return (from u in objDataContext.tblMenus select u);
		    } catch (Exception ex)
		    {
		        return null;
		    }
	    }
        public tblMenu SelectRecordByCaption(string sCaption)
	    {
		    try {
			    return (from u in objDataContext.tblMenus where u.MenuText  == sCaption  select u).Single();
		    } catch (Exception ex)
		    {
		        return null;
		    }
	    }
        public tblMenu SelectRecordByID(int _id)
        {
            try
            {
                return (from u in objDataContext.tblMenus where u.ID_Menu == _id select u).Single();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public int InsertRecord(tblMenu LocalTable)
        {
            try
            {
                objDataContext.tblMenus.InsertOnSubmit(LocalTable);
                objDataContext.SubmitChanges();
                return LocalTable.ID_Menu;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public void UpdateRecord(tblMenu LocalTable)
        {
            try
            {
                objDataContext.tblMenus.Attach(LocalTable);
                objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable);
                objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void UpdateRecordWithoutAttach(tblMenu LocalTable)
        {
            try
            {
                objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable);
                objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool CheckExists(string sCaption)
        {
            return (objDataContext.tblMenus.Any(m => m.MenuText  == sCaption));
        }
        public CMenuController()
        {
            objDataContext = new QLVTDataContext( );
        }
        public CMenuController(QLVTDataContext  _obj)
        {
            objDataContext = _obj;
        }
    }
}
